'''
创建测试用例
1.导包
2.创建类 (继承unittest.TestCase
3.前置条件 实例化接口
4.后置条件 关闭需要关闭的对象
5.创建测试用例、断言
'''
import unittest

import requests

from api.employee import EmployeeApi
from utils import common_assert


class TestEmp(unittest.TestCase):
    employee_id = None

    def setUp(self):
        self.emp = EmployeeApi()

    def test01_add(self):
        response = self.emp.add_employee()
        print(response.json())
        data = response.json().get("data")
        print(data)
        common_assert(self, response, 200, True, 10000, "成功")
        if response.json().get("code") == 10000:
            TestEmp.employee_id = response.json().get("data").get("id")
            print("TestEmp.employee_id", end="")
            print(TestEmp.employee_id)

    def test01_search(self):
        response = self.emp.search_employee(TestEmp.employee_id)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

    def test01_up(self):
        up_data = {"username": "修改名称"}
        response = self.emp.up_employee(TestEmp.employee_id, up_data)
        print(response.json())
        # 公共断言方法
        common_assert(self, response, 200, True, 10000, "操作成功")
        # 原来的断言方法
        '''     self.assertEqual(200, response.status_code)
                self.assertEqual(True, response.json().get("success"))
                self.assertEqual(10000, response.json().get("code"))
                self.assertIn("操作成功", response.json().get("message"))'''

    def test01_del(self):
        response = self.emp.del_employee(TestEmp.employee_id)
        print(response.json())
        common_assert(self, response, 200, True, 10000, "操作成功")

    def test01_list(self):
        response = self.emp.list_employee()
        print(response.json())
        common_assert(self, response, 200, True, 10000, "操作成功")
